//
//  JNewFeatureCollectionVC.swift
//  JMicroBlog
//
//  Created by Jason on 20/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

let reuseIdentifier = "JNewFeatureCell"

class JNewFeatureCollectionVC: UICollectionViewController {

    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        JLog.i("\(__FUNCTION__) \(view.frame)",false)
        
        // cell的尺寸
        layout.itemSize = view.frame.size
        // 最小cell间距
        layout.minimumInteritemSpacing = 0
        // 最小行间距
        layout.minimumLineSpacing = 0
        // 水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 允许分页
        collectionView?.pagingEnabled = true
        // 水平滚动条不显示
        collectionView?.showsHorizontalScrollIndicator = false
        // 取消弹簧效果
        collectionView?.bounces = false 
        
    }
    
    let  imgCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgCount;
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JNewFeatureCell
        
        // cell的图片索引
        cell.imgIndex = indexPath.item
        
        return cell
    }
    
    // cell都是前面的那个
    // cell滚动完全结束时
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last as? NSIndexPath
        // 最后一张引导图片
        if path?.item == imgCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(path!)
            (cell as! JNewFeatureCell).showStartBtn()
        }
    }
}

class JNewFeatureCell : UICollectionViewCell {
    // 启动的引导图片
    @IBOutlet weak var launchImage: UIImageView!
    
    // 马上体验的button
    @IBOutlet weak var startBtn: UIButton!

    //
    var imgIndex : Int = 0 {
        didSet {
            launchImage.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            startBtn.hidden = true
        }
    }
    
    func showStartBtn() {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        
        
        /**
        iOS7.0推出,物理仿真效果
        
        */
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 8.0, options: nil, animations: {
            self.startBtn.transform = CGAffineTransformMakeScale(1.0, 1.0)
        }) { (_) in }
    }
    
    // 点击马上体验的button
    @IBAction func clickStartBtn() {
        NSNotificationCenter.defaultCenter().postNotificationName(JExchangeRootVCNotification, object: "JMain")
        
    }
    
}
